package com.traffic.between.adaptor;

import java.util.Date;

import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.ZhejiangServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel = "zhejiang")
public class ZhejiangAdaptor implements IAdaptor{
	private static Logger log = Logger.getLogger(ZhejiangAdaptor.class);

	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		
		String transNo = orderBean.getIds();
		String pkgNo = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String backUrl = BetweenProperties.CALLBACK_URL+"Zhejiang";

		String result = ZhejiangServiceImpl.service.order(transNo, pkgNo, phoneNo, backUrl);
		log.info("浙江联通后向流量订购接口返回result：【"+result+"】,该订单号为："+transNo);
		JSONObject json = JSONObject.fromObject(result);
		String code = (String) json.get("respCode");
		if ("0000".equals(code)) {

			orderBean.setMessage("订购成功");
			orderBean.setErrorMsg("充值成功");
			orderBean.setStatus("7");
			orderBean.setSuppOrderNo(json.get("orderId") + "");
		} else if ("0001".equals(code)) {
			orderBean.setMessage("订单已受理");
			orderBean.setErrorMsg("订购请求已接受");
			orderBean.setStatus("10");
			orderBean.setSuppOrderNo(json.get("orderId") + "");
			
		}else {
			
			orderBean.setMessage("订购失败");
			orderBean.setErrorMsg(code + "[" + parseResultCodeToMsg(code) + "]");
			orderBean.setStatus("8");
			orderBean.setSuppOrderNo(json.get("orderId") + "");
		}
	
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {
		
		OrderBean orderBean = OrderInfoContext.getContext();
		String orderId = orderBean.getSuppOrderNo();
		log.info("浙江联通后向流量订购状态查询接口orderId：" + orderId);
	
		try {
			if ("".equals(orderId)) {
				orderBean.setMessage("该订单业务流水为空");
				orderBean.setErrorMsg("受理中，无订单");
				orderBean.setStatus("");
			}else {
				String result = ZhejiangServiceImpl.service.orderStatus(orderId);

				JSONObject json = JSONObject.fromObject(result);
				String code = (String) json.get("respCode");
				if ("0000".equals(code)) {
				
					orderBean.setMessage("订购成功");
					orderBean.setErrorMsg("充值成功");
					orderBean.setStatus("7");
					orderBean.setSuppOrderNo(json.get("orderId") + "");
				} else if ("0001".equals(code)) {
					
					orderBean.setMessage("订单已受理");
					orderBean.setErrorMsg("订购请求已接受");
					orderBean.setStatus("1");
					orderBean.setSuppOrderNo(json.get("orderId") + "");
				} else {
					
					orderBean.setMessage("订购失败");
					orderBean.setErrorMsg(code + "[" + parseResultCodeToMsg(code) + "]");
					orderBean.setStatus("8");
					orderBean.setSuppOrderNo(json.get("orderId") + "");
					orderBean.setPhoneNo(json.get("phoneNo") + "");
				}
			}
			
		} catch (Exception e) {

			orderBean.setMessage(e.getMessage());
			orderBean.setErrorMsg(e.getMessage());
			orderBean.setStatus("1");
			
			
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	public String parseResultCodeToMsg(String code) {
		String msg = "";
		if ("0000".equals(code))
			msg = "订购成功.";
		else if ("0001".equals(code))
			msg = "订购请求已接受";
		else if ("1000".equals(code))
			msg = "系统异常，请尽快联系相关运营人员";
		else if ("1001".equals(code))
			msg = "参数错误";
		else if ("1002".equals(code))
			msg = "数字签名验证失败";
		else if ("1003".equals(code))
			msg = "请求服务不存在";
		else if ("1004".equals(code))
			msg = "此地区产品未开通";
		else if ("1005".equals(code))
			msg = "IP地址错误";
		else if ("1006".equals(code))
			msg = "请求时间错误";
		else if ("1007".equals(code))
			msg = "订购请求重复提交";
		else if ("2001".equals(code))
			msg = "用户不存在";
		else if ("2002".equals(code))
			msg = "用户已停机 ";
		else if ("2003".equals(code))
			msg = "用户状态异常（欠费）";
		else if ("2004".equals(code))
			msg = "用户是黑名单";
		else if ("2005".equals(code))
			msg = "该笔订单已完成订购";
		else if ("2006".equals(code))
			msg = "订购关系不存在";
		else if ("2007".equals(code))
			msg = "与已订购的其他产品冲突";
		else if ("2008".equals(code))
			msg = "不允许变更的产品";
		else if ("2009".equals(code))
			msg = "参数错误";
		else if ("2010".equals(code))
			msg = "用户套餐不能订购该业务";
		else if ("2011".equals(code))
			msg = "其他原因";
		else if ("2012".equals(code))
			msg = "订购失败";
		else if ("2013".equals(code))
			msg = "该地区暂不支持2G用户订购";
		else if ("2014".equals(code))
			msg = "无更多的产品订购（用户当月订购流量包已到达叠加上限）";
		else if ("2015".equals(code))
			msg = "暂时无产品订购，针对预付用户";
		else if ("2016".equals(code))
			msg = "2G/3G融合用户不允许订购";
		else if ("2017".equals(code))
			msg = "用户状态异常（资料不全）";
		else if ("2018".equals(code))
			msg = "用户状态异常(不在有效期)";
		else if ("2019".equals(code))
			msg = "用户主套餐变更，当月不能订购";
		else if ("2020".equals(code))
			msg = "用户服务密码为初始密码";
		else if ("2021".equals(code))
			msg = "用户有正在处理订单";
		else if ("2022".equals(code))
			msg = "该地区暂不支持4G用户订购";
		else if ("2023".equals(code))
			msg = "用户状态异常（未查到用户类型）";
		else if ("2024".equals(code))
			msg = "该地区暂不支持3G用户订购";
		else if ("3001".equals(code))
			msg = "appkey验证无效";
		else if ("3002".equals(code))
			msg = "账户余额不足，请尽快联系相关运营人员";
		else if ("3003".equals(code))
			msg = "账户余额不足，请尽快联系相关运营人员";
		else if ("3004".equals(code))
			msg = "产品编号无效";
		else if ("3005".equals(code))
			msg = "剩余的订购关系数目不足";
		else if ("3006".equals(code))
			msg = "此地区产品未开通";
		else if ("3007".equals(code))
			msg = "活动信息不存在";
		else if ("3008".equals(code))
			msg = "活动已过期";
		else if ("3009".equals(code))
			msg = "活动不包含此产品";
		else if ("3010".equals(code))
			msg = "没有pkgNo的订购权限";
		else if ("3011".equals(code))
			msg = "订单信息已存在，但未提交订购";
		else if ("3201".equals(code))
			msg = "代理商暂停使用";
		else if ("3211".equals(code))
			msg = "代理商交付能力鉴权失败";
		else if ("3212".equals(code))
			msg = "代理商交付能力暂停使用";
		else if ("3213".equals(code))
			msg = "代理商交付密码无效";
		else if ("3221".equals(code))
			msg = "代理商业务鉴权失败";
		else if ("3222".equals(code))
			msg = "代理商业务暂停服务";
		else if ("3223".equals(code))
			msg = "代理商产品暂停服务";
		else if ("3224".equals(code))
			msg = "代理商产品适用手机号鉴权失败";
		else if ("5201".equals(code))
			msg = "订购能力鉴权失败";
		else if ("5202".equals(code))
			msg = "订购能力暂停使用";
		else if ("5211".equals(code))
			msg = "业务鉴权失败";
		else if ("5212".equals(code))
			msg = "业务暂停使用";
		else if ("5221".equals(code))
			msg = "产品鉴权失败";
		else if ("5222".equals(code))
			msg = "产品暂停使用";
		else if ("5223".equals(code))
			msg = "产品适用地区不匹配";
		else if ("5231".equals(code))
			msg = "供应商产品鉴权失败";
		else if ("5232".equals(code))
			msg = "供应商产品暂停使用";
		else if ("5233".equals(code))
			msg = "供应商产品地区不匹配";
		else if ("5241".equals(code))
			msg = "供应商鉴权失败";
		else if ("5242".equals(code))
			msg = "供应商暂停服务";
		else if ("4001".equals(code))
			msg = "客户端流水号请求重复订购";
		else if ("9001".equals(code))
			msg = "等待响应消息超时";
		else if ("9002".equals(code))
			msg = "网络异常";
		else if ("9003".equals(code))
			msg = "数据库连接异常";
		else if ("9101".equals(code))
			msg = "系统错误";
		else if ("9102".equals(code))
			msg = "网络异常";
		else if ("9999".equals(code))
			msg = "其他原因";
		else {
			msg = "other fail reason";
		}
		return msg;
	}
}